/**
 * Created by Tang on 2017/7/12.
 */
function Book(name, author, price, classify, company, time) {
    this.name = name;
    this.author = author;
    this.price = price;
    this.classify = classify;
    this.company = company;
    this.time = time;
};
function Manager() {
    this.createTable();
    this.arr = [];
};
Manager.prototype = {
    //页面创建方法;
    createTable:function () {
        var _this = this;
        $.ajax({
            type:'get',
            url:'http://localhost:2403/books',
            success:function (data) {
                for (var i in data){
                    $('<tr>').html(
                        '<td><input type="checkbox"></td>' +
                        '<td>' + data[i].name + '</td>' +
                        '<td>' + data[i].name + '</td>' +
                        '<td>' + data[i].author + '</td>' +
                        '<td>' + data[i].price + '</td>' +
                        '<td>' + data[i].classify + '</td>' +
                        '<td>' + data[i].time + '</td>' +
                        '<td><button class="_show">查看</button>' +
                        '<button class="_change">修改</button></td>')
                        .attr('id',data[i].id)
                        .appendTo($('.mR_warp_table tbody'));
                    _this.arr.push(data[i].name);
                }
            },
            error:function (er) {
                console.log(er)
            }
        })
    },
    //创建书方法;
    createBook:function(cls){
        return book = new Book(
            $('.' + cls + ' :text').eq(0).val(),
            $('.' +  cls+ ' :text').eq(1).val(),
            $('.' + cls + ' :text').eq(2).val(),
            $('.' +  cls+ ' :text').eq(3).val(),
            $('.' + cls + ' :text').eq(4).val(),
            $('.' +  cls+ ' :text').eq(5).val()
        );
    },
    //添加书方法;
    addBook:function (fn) {
        var _this = this;
        $.ajax({
            url:'http://localhost:2403/books',
            type:'post',
            data:_this.createBook('init'),
            success:fn,
            error:function (err) {
                console.log(err);
                alert('添加失败')
            }
        });
    },
    //删除方法;
    delName:function (id,fn) {
        $.ajax({
            type:'delete',
            url:'http://localhost:2403/books/'+id,
            success:fn,
            error:function (err) {
                console.log(err);
                alert('删除失败')
            }
        })
    },
    //修改方法
    change:function (id,fn) {
        var _this = this;
        $.ajax({
            type:'put',
            url:'http://localhost:2403/books/'+id,
            data:_this.createBook('change'),
            success:fn,
            error:function (err) {
                console.log(err);
                alert('修改失败')
            }
        })
    }
};